<?php
//面板设置页面
function baidupan_setting_control()
{
	$error = new WP_Error;

	$page = admin_url('admin.php?page=baidupan-setting');
	$schedule = wp_get_schedule('baidupan');

	if (isset($_POST['action']) && 'setting' == $_POST['action']) {
		$appname = sanitize_text_field($_POST['appname']);
		$appkey = sanitize_text_field($_POST['appkey']);
		$secretkey = sanitize_text_field($_POST['secretkey']);
		if ($appname && $appkey && $secretkey) {
			update_option('baidupan_appname', $appname);
			update_option('baidupan_appkey', $appkey);
			update_option('baidupan_secretkey', $secretkey);
		} else {
			delete_option('baidupan_appname');
			delete_option('baidupan_appkey');
			delete_option('baidupan_secretkey');
		}
		$cycle = $_POST['cycle'];
		if ($schedule != $cycle) {
			baidupan_delete_cron();
			if ($cycle != 'off') {
				wp_schedule_event(strtotime(date('Y-m-d')), $cycle, 'baidupan');
			}
			$schedule = $cycle;
		}

		$error->add('info', '设置保存成功！');
	}

	$baidupan = new Pcs;
	if (!empty($_GET['code'])) {
		$token = $baidupan->accessToken($_GET['code']);
		if ($token) {
			$error->add('info', '授权成功！');
		} else {
			$error->add('error', $baidupan->getError());
		}
	}

	$quota = $baidupan->getQuota();
	if ($quota) {
		$error->add('info', '空间容量：' . $quota['used'] . '/' . $quota['total']);
	}

	$baidupan_appname = get_option('baidupan_appname');
	$baidupan_appkey = get_option('baidupan_appkey');
	$baidupan_secretkey = get_option('baidupan_secretkey');
?>

	<link rel="stylesheet" href="<?php echo plugins_url('baidupan/css/control.css'); ?>">

	<div class="mywrap wp-clearfix">
		<?php
		if ($error->has_errors()) {
			if ($error->get_error_message('error')) {
				echo '<div class="notice is-dismissible error">' . $error->get_error_message('error') . '</div>';
			} else {
				echo '<div class="notice is-dismissible success">' . $error->get_error_message('info') . '</div>';
			}
		}
		?>

		<form class="entry" method="post" action="<?php echo $page; ?>">
			<table class="form-table">
				<tbody>
					<tr>
						<th scope="row"><label for="cycle">定时</label></th>
						<td>
							<select name="cycle" id="cycle">
								<option value="daily" <?php if ('daily' == $schedule) echo ' selected'; ?>>每天</option>
								<option value="weekly" <?php if ('weekly' == $schedule) echo ' selected'; ?>>每周</option>
								<option value="monthly" <?php if ('monthly' == $schedule) echo ' selected'; ?>>每月</option>
								<option value="off" <?php if (!$schedule || 'off' == $schedule) echo ' selected'; ?>>关闭定时</option>
							</select>
							<p class="description">定时备份仅针对数据库，附件是实时同步到<a href="https://pan.baidu.com" target="_blank" rel="noopener nofollow">百度网盘</a>的。</p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="appname">应用名称</label>
						</th>
						<td>
							<input id="appname" name="appname" class="regular-text code" type="text" value="<?php echo $baidupan_appname; ?>">
							<p class="description">访问<a href="https://pan.baidu.com/union/console/applist" target="_blank" rel="noopener nofollow">百度网盘开放平台</a>获取。</p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="appkey">AppKey</label>
						</th>
						<td>
							<input id="appkey" name="appkey" class="regular-text code" type="text" value="<?php echo $baidupan_appkey; ?>">
							<p class="description">访问<a href="https://pan.baidu.com/union/console/applist" target="_blank" rel="noopener nofollow">百度网盘开放平台</a>获取。</p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="secretkey">SecretKey</label>
						</th>
						<td>
							<input id="secretkey" name="secretkey" class="regular-text code" type="text" value="<?php echo $baidupan_secretkey; ?>">
						</td>
					</tr>
				</tbody>
			</table>
			<p>
				<input class="button-primary" type="submit" value="保存设置">
				<?php
				if ($baidupan_appkey && $baidupan_secretkey && !$quota) {
					echo '<a class="button" href="https://openapi.baidu.com/oauth/2.0/authorize?response_type=code&amp;client_id=' . $baidupan_appkey . '&amp;scope=basic,netdisk&amp;redirect_uri=' . urlencode($page) . '">授权应用</a>';
				}
				?>
				<input type="hidden" name="action" value="setting">
			</p>
		</form>
	</div>

<?php } ?>